﻿using Liang.DataBase.BLL;
using Liang.DataBase.Helper;
using Liang.DataBase.Model.Entity;
using Liang.DataBase.Tools;
using Liang.Model;
using Liang.UI;
using Liang.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Client
{
    public partial class FrmAppEdit : Form
    {
        public sys_app app { get; set; }

        AppBLL appBLL = new AppBLL();

        public FrmAppEdit()
        {
            InitializeComponent();
        }

        public FrmAppEdit(sys_app app)
        {
            InitializeComponent();
            this.app = app;
        }

        private void FrmModuleAppEdit_Load(object sender, EventArgs e)
        {
            initCombox();

            if (app != null)
            {
                this.Text = "功能管理-修改";
                this.txtAppName.Text = app.name;
                this.txtAppName.ReadOnly = true;
                this.txtImgUrl.Text = app.imgUrl;
                comAppUrl.Enabled = false;
                itemTextValueByValue(comAppUrl, app.appUrl);
                SetComboBoxYyjb(comYyjb, app.basicdataId.ToString());
            }
            else
            {
                this.Text = "功能管理-新增";
            }
        }

        public void initCombox()
        {
            var listApp = appBLL.FindAll();

            // 初始化程序路径
            List<ItemTextValue> lists = getAllForms();
            if (app == null)
            {
                for (int i = 0; i < listApp.Count; i++)
                {
                    lists.Remove(lists.Where(p => p.selectText == listApp[i].appUrl).FirstOrDefault());
                }

            }
            this.comAppUrl.DisplayMember = "selectValue";
            this.comAppUrl.ValueMember = "selectText";
            this.comAppUrl.DataSource = lists;

            // 初始化功能级别
            BasicdataBLL basicdataBLL = new BasicdataBLL();
            var list = basicdataBLL.FindAll(s => s.code.Contains("YYJB"));
            this.comYyjb.DataSource = list;
            this.comYyjb.DisplayMember = "des";
            this.comYyjb.ValueMember = "basicdataId";
        }

        void SetComboBoxYyjb(ComboBox cb, string Value)
        {
            for (int i = 0; i < cb.Items.Count; ++i)
            {
                sys_basicdata o = (sys_basicdata)cb.Items[i];

                if (o.basicdataId.ToString().Equals(Value))
                {
                    cb.SelectedIndex = i;
                    break;
                }
            }
        }

        public bool validate()
        {
            if (string.IsNullOrEmpty(this.txtAppName.Text))
            {
                MessageBoxEx.Show(this, "请输入功能名称。");
                this.txtAppName.Focus();
                return false;
            }

            if (null == this.comAppUrl.SelectedItem || "".Equals(comAppUrl.SelectedItem.ToString()))
            {
                MessageBoxEx.Show(this, "请选择程序URL。");
                this.comAppUrl.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(this.txtImgUrl.Text))
            {
                MessageBoxEx.Show(this, "请输入功能图片路径。");
                this.txtImgUrl.Focus();
                return false;
            }

            if (app == null)
            {
                var where = LambdaHelper.True<sys_app>().And(q => q.name.Equals(this.txtAppName.Text));
                sys_app tmp = appBLL.FindSingle(where);

                if (null != tmp)
                {
                    MessageBoxEx.Show(this, "功能名称重复，请重新输入。");
                    this.txtAppName.Focus();
                    return false;
                }
            }
            return true;
        }
  

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                if (app == null)
                {
                    app = new sys_app();
                    app.name = this.txtAppName.Text;
                    app.appUrl = ((ItemTextValue)comAppUrl.SelectedItem).selectText;
                    app.imgUrl = this.txtImgUrl.Text;
                    app.basicdataId = ((sys_basicdata)comYyjb.SelectedItem).basicdataId;
                    appBLL.Insert(app);
                }
                else
                {
                    app.name = this.txtAppName.Text;
                    app.appUrl = ((ItemTextValue)comAppUrl.SelectedItem).selectText;
                    app.imgUrl = this.txtImgUrl.Text;
                    app.basicdataId = ((sys_basicdata)comYyjb.SelectedItem).basicdataId;
                    appBLL.Update(app, app.appId);
                }

                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 获取所有app
        /// </summary>
        /// <returns></returns>
        List<ItemTextValue> getAllForms()
        {
            List<ItemTextValue> names = new List<ItemTextValue>();
            string[] ns = Const.NAMESPACE.Split(',');
            for (int i = 0; i < ns.Length; i++)
            {
                foreach (var type in System.Reflection.Assembly.Load(ns[i]).GetTypes())
                {
                    if (typeof(DockContent).IsAssignableFrom(type))
                    {
                        try
                        {
                            using (DockContent form = Activator.CreateInstance(type) as DockContent)
                            {
                                ItemTextValue it = new ItemTextValue(form.Text, form.ToString().Split(',')[0]);
                                names.Add(it);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.WriteLog(typeof(UserManager), ex);
                        }
                    }
                }
            }
            //LogHelper.WriteLog(typeof(UserHelper),names.ToString() ,3);
            return names;
        }

        void itemTextValueByValue(ComboBox cb, string Value)
        {
            for (int i = 0; i < cb.Items.Count; ++i)
            {
                ItemTextValue o = (ItemTextValue)cb.Items[i];
                if (o.selectText == Value)
                {
                    cb.SelectedIndex = i;
                    break;
                }
            }
        }
    }
}
